package edu.bupt.e1btree;

public class T47SumRootToLeafNumbers {

    private int sum;

    public int sumNumbers(TreeNode root) {
        if (null == root) return 0;

        dfs(root,0);
        return sum;
    }

    private void dfs(TreeNode root, int curSum){
        // 终止条件
        if(null == root.left && null == root.right){
            sum += curSum*10 + root.val;
            return;
        }

        int temp = curSum*10 + root.val;
        // 扩展
        if(null != root.left) dfs(root.left, temp);
        if (null != root.right) dfs(root.right,temp);
    }
}
